﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

namespace Zhongy
{
    public class PlayerHandler :IHandler
    {
        private UserCache userCache = Cache.User;
        private PlayerCache playerCache = Cache.Player;
        public override void OnDisconnect(ClientPer client)
        {
            playerCache.SubAllPlayer();
        }

        public override void OnReceive(ClientPer client, SocketReceiveMsg msg)
        {
            switch (msg.MT)
            {
                case OpCode.TYPE_SYN_PLAYER_INFO:
                    SyncPlayerInfo(client, msg);
                    break;

                case OpCode.TYPE_ADD_STU_C:
                    AddPlayer(client, msg);
                    break;

                case OpCode.TYPE_DEL_STU_C:
                    SubPlayer(client, msg);
                    break;

                case OpCode.TYPE_STU_DATA:
                    SyncScoreDta(client, msg);
                    break;

                default:
                    Debug.LogError("信息归属不对");
                    break;
            }
        }

        /// <summary>
        /// 同步玩家信息
        /// </summary>
        private void SyncPlayerInfo(ClientPer client, SocketReceiveMsg msg)
        {
            SingleExecute.Instance.Execute(() =>
                {
                    if (msg.Player != null && msg.Player.Count > 0)
                    {
                        playerCache.SyncPlayerState(client, msg.Player);
                    }
                }
            );
        }

        /// <summary>
        /// 同步玩家分数
        /// </summary>
        private void SyncScoreDta(ClientPer client, SocketReceiveMsg msg)
        {
            SingleExecute.Instance.Execute(() =>
            {
                if (msg.Score != null && msg.Score.Count > 0)
                {
                    playerCache.SyncPlayerScoreData(client, msg.Score);
                }

            });
        }

        private void AddPlayer(ClientPer client, SocketReceiveMsg msg)
        {
            SingleExecute.Instance.Execute(() =>
            {
                if (!string.IsNullOrEmpty(msg.ID))
                {
                    PlayerModel player = userCache.IsExsitUser(msg.ID)
                        ? new PlayerModel(Cache.User.GetUser(msg.ID))
                        : new PlayerModel(msg.ID);

                    playerCache.AddPlayer(client, player);

                    SocketSendMsg sendMsg = new SocketSendMsg
                    {
                        MT = OpCode.TYPE_ACK2C,
                        ackid = msg.mesgid,
                        result = "OK",
                        mesg = string.Empty
                    };

                    Dispatch(TransCode.Net, NetEvtCode.Client2TcpMsg, new object[] { client, sendMsg });

                    //todo 增加玩家 游戏内容暂时不做处理

                    //如果结束面板跳回玩家连接面板
                    //Debug.LogError("111");
                    Dispatch(TransCode.Game, GameEvtCode.GameConnect);

                }


            });
        }

        private void SubPlayer(ClientPer client, SocketReceiveMsg msg)
        {
            SingleExecute.Instance.Execute(() =>
            {
                if (!string.IsNullOrEmpty(msg.ID))
                {
                    playerCache.SubPlayer(msg.ID);

                    SocketSendMsg sendMsg = new SocketSendMsg
                    {
                        MT = OpCode.TYPE_ACK2C,
                        ackid = msg.mesgid,
                        result = "OK",
                        mesg = string.Empty
                    };

                    Dispatch(TransCode.Net, NetEvtCode.Client2TcpMsg, new object[] { client, sendMsg });

                    //如果结束面板跳回玩家连接面板
                    //Debug.LogError("111");

                    Dispatch(TransCode.Game, GameEvtCode.GameConnect);
                }

            });
        }
        
    }
}
